<?php

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    public function index(){
        $this->load->helper('form');
        $data["js"] = Array('login.js'); 
        $this->load->view(__TEMPLATES_PATH__."/login",$data);
        
    }
    public function check(){
        $error = Array();
       $this->load->helper('security');
       $this->load->Model("mlogin");
       $this->load->library('form_validation','security');
       $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[50]|xss_clean');
       $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|md5');
    if ($this->form_validation->run() == FALSE)
            {
			$error[] =   validation_errors();
        }
        $username = $this->security->sanitize_filename($this->input->post('username'));
        $password = do_hash($this->security->sanitize_filename($this->input->post('password')),'md5');
      $resuilt =  $this->mlogin->getusers($username,$password);
      if($resuilt == "0"){
          $error[] = "Tài khoản : ".$username." . Hoặc mật khẩu của bạn không đúng , vui lòng nhập lại";
      }
                 $status = "error";
   if(count($error)==0){
         $admin_login = array(
                   'username'  => $_POST['username'],
                   'logged_in' => TRUE
               );
         $this->session->set_userdata($admin_login);
       $status = "success";
     


   }
                
             $reponsive = array("status"=> $status,"msg"=>$error);
                 $jsonstring = json_encode($reponsive);
                 echo $jsonstring;
    }


                }